package cn.huanba.service;

import cn.huanba.dao.DockDao;
import cn.huanba.dao.PictureDao;
import cn.huanba.dao.ProductDao;
import cn.huanba.entity.Dock;
import cn.huanba.entity.Picture;
import cn.huanba.entity.Product;
import cn.huanba.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Product)表服务实现类
 *
 * @author makejava
 * @since 2021-01-07 17:23:27
 */
@RestController
@RequestMapping("product")
public class ProductServiceImpl {
    @Resource
    private ProductDao productDao;
    @Resource
    private PictureDao pictureDao;
    @Autowired
    private OssUtils ossUtils;
    @Resource
    private DockDao dockDao;

    /**
     * 替换图片路径为阿里的oss
     *
     * @param product
     */

    public void setPic(@RequestBody Product product) {
        product.getPictureList().stream().forEach(e -> e.setPath(ossUtils.createFileName(e.getPath())));
    }

    @RequestMapping("getProductAndDock")
    public List<Product> getProductAndDock(@RequestParam("uid") Integer uid) {
        return productDao.getProductAndDock(uid);
    }

    @RequestMapping("saveDock")
    public int saveDock(@RequestBody Dock dock) {
        return dockDao.insert(dock);
    }

    @RequestMapping("getProductListByTid")
    public List<Product> getProductListByTid(@RequestParam("tid") Integer tid) {
        Product product = new Product();
        product.setTid(tid);
        List<Product> products = productDao.queryAll(product);
        products.stream().forEach(e -> setPic(e));
        return products;
    }

    @RequestMapping("getProductListByUser")
    public List<Product> getProductListByUser(@RequestParam("uid") Integer uid) {
        Product product = new Product();
        product.setUid(uid);
        List<Product> products = productDao.queryAll(product);
        products.stream().forEach(e -> setPic(e));
        return products;
    }

    @RequestMapping("queryAllByproductName")
    public List<Product> queryAllByproductName(@RequestParam("productName") String productName) {
        Product product = new Product();
        product.setProductname(productName);
        List<Product> products = productDao.queryAll(product);
        products.stream().forEach(e -> setPic(e));
        return products;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param pid 主键
     * @return 实例对象
     */
    @RequestMapping("queryById")
    public Product queryById(@RequestParam("pid") Integer pid) {
        Product product = this.productDao.queryById(pid);
        setPic(product);
        return product;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @RequestMapping("queryAllByLimit")
    public List<Product> queryAllByLimit(@RequestParam("offset") int offset, @RequestParam("limit") int limit) {
        return this.productDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param product 实例对象
     * @return 实例对象
     */
    @RequestMapping("insert")
    public int insert(@RequestBody Product product) {
        int proResult= this.productDao.insert(product);
        int picResult=0;
        if(proResult>0){
            for (Picture picture : product.getPictureList()) {
                picture.setPid(product.getPid());
                picResult=pictureDao.insert(picture);
                if(picResult==0)
                    return 0;
            }
        }
        return proResult;
    }

    /**
     * 修改数据
     *
     * @param product 实例对象
     * @return 实例对象
     */
    @RequestMapping("update")
    public Product update(@RequestBody Product product) {
        this.productDao.update(product);
        return this.queryById(product.getPid());
    }

    /**
     * 通过主键删除数据
     *
     * @param pid 主键
     * @return 是否成功
     */
    @RequestMapping("deleteById")
    public boolean deleteById(@RequestParam("pid") Integer pid) {
        return this.productDao.deleteById(pid) > 0;
    }
}